package com.liu.npc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.npc.entity.base.NpcBasicsInfo;
import com.liu.npc.entity.base.NpcOperateRelation;
import com.liu.npc.entity.qo.AddNpcQO;
import com.liu.npc.mapper.NpcBasicsInfoMapper;
import com.liu.npc.service.INpcBasicsInfoService;
import com.liu.npc.service.INpcOperateRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * NPC基础信息表 服务实现类
 * </p>
 *
 * @author LiuChengLong
 * @since 2022-11-13
 */
@Service
public class NpcBasicsInfoServiceImpl extends ServiceImpl<NpcBasicsInfoMapper, NpcBasicsInfo> implements INpcBasicsInfoService {

    @Resource
    private INpcOperateRelationService npcOperateRelationService;

    @Override
    public void saveNpc(AddNpcQO addNpcQO) {
        // 获取所有npc
        List<NpcBasicsInfo> npcBasicsInfoList = baseMapper.selectList(Wrappers.emptyWrapper());
        List<String> npcNameList = npcBasicsInfoList.stream().map(NpcBasicsInfo::getNpcName).collect(Collectors.toList());
        if (!npcNameList.contains(addNpcQO.getNpcName())){
            // 插入npc基本信息
            baseMapper.insert(addNpcQO);
            QueryWrapper<NpcBasicsInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("npc_name", addNpcQO.getNpcName());
            // 获取本次插入npc的id
            NpcBasicsInfo npcBasicsInfo = baseMapper.selectOne(wrapper);
            // 拼接npc操作关联信息
            List<NpcOperateRelation> npcOperateRelationList = new ArrayList<>();
            List<Integer> operateIdList = addNpcQO.getOperateIdList();
            operateIdList.forEach(o->{
                NpcOperateRelation npcOperateRelation = new NpcOperateRelation();
                npcOperateRelation.setNpcId(npcBasicsInfo.getId());
                npcOperateRelation.setOperateId(o);
                npcOperateRelationList.add(npcOperateRelation);
            });
            // 插入npc操作关联
            npcOperateRelationService.saveBatch(npcOperateRelationList);
        }
    }

    @Override
    public void updateNpc(AddNpcQO addNpcQO) {
        // 更新npc信息
        baseMapper.updateById(addNpcQO);
        // 删除久npc操作关联
        QueryWrapper<NpcOperateRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("npc_id", addNpcQO.getId());
        npcOperateRelationService.remove(wrapper);
        // 拼接npc操作关联信息
        List<NpcOperateRelation> npcOperateRelationList = new ArrayList<>();
        List<Integer> operateIdList = addNpcQO.getOperateIdList();
        operateIdList.forEach(o->{
            NpcOperateRelation npcOperateRelation = new NpcOperateRelation();
            npcOperateRelation.setNpcId(addNpcQO.getId());
            npcOperateRelation.setOperateId(o);
            npcOperateRelationList.add(npcOperateRelation);
        });
        // 插入npc操作关联
        npcOperateRelationService.saveBatch(npcOperateRelationList);
    }
}
